'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorChooseinfoCtrl
 * @description
 * # AdministratorChooseinfoCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AdministratorChooseinfoCtrl', function($scope, $rootScope, $window, $routeParams, $http, Util, openApi, localStorageService, PagingService) {
        var param = {};
        param.callback = function(data) {
            $scope.selectLists.forEach(function(item, index) {
                data.forEach(function(subitem, index) {
                    // 设置选中状态
                    if (item.uid == subitem.uid) {
                        subitem.selected = true;
                    }
                });
            })
        };
        var filterId = "pkId";
        $scope.selectLists = []; //已经选择了的数组，用来回传
        // -----------------底部款项类型选框添加end-----------------------
        $scope.choosePeople = function(item) {
            $scope.searchInput = "";
            item.selected = !item.selected;
            // 同步选择的数据
            if (item.selected) {
                $scope.selectLists.push(item)
            } else {
                $scope.selectLists = $scope.selectLists.filter(function(subitem, index) {
                    return item.uid != subitem.uid
                })
            }
        };
        // 搜索
        $scope.getData = function() {
            var relatedModule;
            relatedModule = getParamUrl(); //获取借印章或者还印章
            // 出场申请接口不一样，参数也不一样，过滤条件也不一样
            if (relatedModule == "certificateType") {
                var porlist = {
                    "人员姓名": {
                        "name": "ownerName"
                    },
                    "证件类别": {
                        "name": "certificateType"
                    }
                }
                param.ownerName = $scope.searchInput || ""
                var config = {};
                config.porlist = porlist;
                config.searchName = "姓名"
                $scope.taskList = new PagingService(openApi.attendanceCer, param, config);
            } else {
                param.filterSql = $scope.searchInput || "";
                param.relatedModule = relatedModule || "";
                $scope.taskList = new PagingService(openApi.getRelpage, param);
            }
        };
        // 根据不同的id请求的列表地址会不同
        var getParamUrl = function() {
            var url, form_id;
            form_id = $routeParams.id;
            if (form_id == "yysq") {
                $scope.title = "用印申请";
                // 用印申请
                url = "select_seal_by_use";
            } else   if (form_id == "jyz") {
                  $scope.title = "印章字样";
                  // 借印章
                  url = "sealBorrow_select_seal";
              } else if (form_id == "hyz") {
                $scope.title = "印章字样";
                // 还印章
                url = "sealReturn_select_seal";
            } else if (form_id == "ccsq") {
                //  出场申请的话过滤id不一样
                $scope.title = "出场人员";
                filterId = "cerId";
                url = "certificateType";
            }
            return url;
        };
        $scope.search = function() {
            // if (!param.filterSql) {
            //     Util.Toast("请输入印章字样");
            //     return;
            // }
            $scope.getData();
        };
        // 获取数据返回
        $scope.submit = function() {
            if ($scope.selectLists.length < 1) {
                Util.Toast("请选择");
                return;
            }
            localStorageService.set("selectLists", $scope.selectLists);
            $window.history.back();
        };
        // 页面初始化请求数据
        var init = function() {
            $scope.getData();
            $scope.selectLists = Util.getSelectLists();
        };
        init();
    });
